<?php

namespace backend\models;
use common\models\UserInfo;
use Yii;

/**
 * This is the model class for table "log_pay".
 *
 * @property string $prepay_id
 * @property string $transaction_id
 * @property string $trade_no
 * @property integer $uid
 * @property integer $type
 * @property integer $starttime
 * @property integer $endtime
 * @property string $memo_xml
 * @property string $rmb
 * @property integer $state
 * @property integer $state_count
 */
class LogPay extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
	public $amount;
	public $transImage;
	public $account;
	public $accountName;
	public $create_time;
	public $address;
	public $price_state;
	public $price_modify;
    public static function tableName()
    {
        return 'log_pay';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['prepay_id'], 'required'],
            [['uid', 'type', 'starttime', 'endtime', 'state', 'state_count'], 'integer'],
            [['memo_xml','trans_image'], 'string'],
            [['rmb'], 'number'],
            [['prepay_id', 'transaction_id'], 'string', 'max' => 128],
            [['trade_no'], 'string', 'max' => 64],
            [['transaction_id'], 'unique'],
            [['trade_no'], 'unique']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'prepay_id' => 'Prepay ID',
            'transaction_id' => 'Transaction ID',
            'trade_no' => 'Trade No',
            'uid' => 'Uid',
            'type' => 'Type',
            'starttime' => 'Starttime',
            'endtime' => 'Endtime',
            'memo_xml' => 'Memo Xml',
            'rmb' => 'Rmb',
            'state' => 'State',
            'state_count' => 'State Count',
        ];
    }
    public static function payedAmount($trade_no){
    	$amountInfo = self::findBySql("select sum(rmb) as amount from log_pay where trade_no='{$trade_no}' AND state in (1,2)")->one();
    	return floatval($amountInfo['amount']);
    }
    //外链订单信息
    public function getPayTradeInfo()
    {
    	return $this->hasOne(PayTrade::className(),['trade_no'=>'trade_no']);
    }
    //外连用户信息
    public function getUserInfo()
    {
        return $this->hasOne(UserInfo::className(),['id'=>'uid']);
    }
}
